//1.引入express框架
const express = require("express")
//创建网站服务器
const app = express()
//处理状态资源
const bodyParser = require('body-parser')
app.use(express.static('public'))
//处理参数
app.use(bodyParser.urlencoded())//获取post请求的参数 urlencoded
app.use(bodyParser.json());
//设置运行跨域访问服务器
app.all("*", function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With')
  res.header("Access-Control-Allow-Headers", ['mytoken', 'Content-Type'])
  next()
})
//路由
/*登录请求*/
app.post("/data", (req, res) => {
  let username = req.body.username;
  let password = req.body.password;
  if (username === 'admin' && password === '123456') {
    res.send({
      status: 200,
      data: '登录成功'
    })
  } else {
    res.send({
      status: 500,
      data: '登录失败'
    })
  }
})
/*侧边栏请求*/
app.get("/data", (req, res) => {
  res.send(
    {
      data: [{
        id: 1, authName: '个人中心',
        children: [
          {id: 101, authName: '个人信息', path: 'main'},
        ]
      },
        {
          id: 2, authName: '用户管理', children: [
            {id: 201, authName: '用户列表', path: 'user'},
          ]
        },
        {
          id: 3, authName: '商品信息', children: [
            {id: 301, authName: '商品列表', path: 'goods'},
          ]
        },
        {
          id: 4, authName: '数据统计',
          children: [
            {id: 401, authName: '数据展示', path: 'manager'},
          ]
        },
        {
          id: 5, authName: '物流详情',
          children: [
            {id: 501, authName: '快递详情', path: 'progress'},
          ]
        },
      ],

      mata: {
        msg: '获取用户超过',
        status: 200
      }
    }
  )
})
/*内容区域*/
const main = [
  {id: '101', name: '华为', price: 2888},
  {id: '102', name: '小米', price: 2777},
  {id: '103', name: 'vivo', price: 1666},
  {id: '104', name: '苹果', price: 3899},
  {id: '105', name: '诺基亚', price: 1599},
  {id: '106', name: '金星', price: 999},
  {id: '107', name: '菠萝', price: 1088},
  {id: '107', name: '大哥大', price: 5000},
]
app.get("/rightMain", (req, res) => {
  res.send(main)
})

/*用户列表*/
const userList = [
  {id: '1001', name: '超级管理员', username: 'admin', create_time: 19990101, mg_state: true, email: '184@qq.com'},
  {id: '1002', name: '用户小强', username: 'xiaoqiang', create_time: 19990101, mg_state: true, email: '158@qq.com'},
  {id: '1003', name: '用户小明', username: 'xiaoming', create_time: 20000202, mg_state: true, email: '186@qq.com'},
  {id: '1004', name: '用户小花', username: 'xiaohua', create_time: 19990101, mg_state: true, email: '158@qq.com'},
  {id: '1005', name: '用户小胖', username: 'xiaopang', create_time: 19990101, mg_state: true, email: '158@qq.com'},
]
app.get("/users", (req, res) => {

  res.send({
    data: {
      totalPage: userList.length,
      pagenum: 1,
      user: userList,
      meta: {
        status: '200',
        message: '获取信息成功',
      }
    }
  })
})
/*修改状态*/
app.put("/users/:id/state/:mg_state", (req, res) => {
    uid=req.params.id
    mg_state=req.params.mg_state
  for (let i = 0; i <userList.length ; i++) {
     if (userList[i].id==uid){
       userList[i].mg_state=mg_state
     }
  }
  res.send({
    data: {
      id: uid,
      mg_state: mg_state,
    },
    meta: {
      msg: "设置状态成功",
      status: 200
    }
  })

})
// /*添加用户*/
app.post("/add", (req, res) => {
  let userMsg = req.body.userMsg
  userList.push(userMsg)
  res.send({
    data: {
      users: userList
    },
    meta: {
      msg: "添加数据成功",
      status: 200
    }
  })
})
/*同步用户信息*/
app.post("/upUser", (req, res) => {
  let uid = req.body.params;
  res.send({
    data: {
      id: uid,
    },
    meta: {
      msg: "查询成功",
      status: 200
    }
  })
})
/*修改用户信息*/
app.put("/update/:id", (req, res) => {
  let uid = req.params.id;
  let password = req.body.password
  let email = req.body.email
  for (let i = 0; i <userList.length ; i++) {
     if (userList[i].id==uid){
       userList[i].create_time=password
       userList[i].email=email
     }
  }
  res.send({
    data: {
      /*测试用的可有可无*/
      uid: uid,
      password: password,
      email: email
    },
    meta: {
      msg: "查询成功",
      status: 200
    }
  })
})
/*删除用户*/
app.delete("/delUser/:id", (req, res) => {
  let uid = req.params.id;
  userList.map((item, index) => {
    if (item.id == uid) {
      userList.splice(index, 1)
    }
  })
  res.send({
    data: {
      id: uid
    },
    meta: {
      msg: "获取用户信息成功",
      status: 200
    }
  })
})
/*获取商品列表*/
const goodList = [
  {id: '101', name: '芒果', money: '10/斤', level: '0', path: 'mango'},
  {id: '102', name: '火腿肠', money: '15/斤', level: '0', path: 'ham'},
  {id: '103', name: '苹果', money: '5/斤', level: '0', path: 'apple'},
  {id: '104', name: '毛衣', money: '48/件', level: '2', path: 'sweater'},
  {id: '105', name: '短袖', money: '30/件', level: '1', path: 'short'},
  {id: '106', name: 'T恤', money: '40/件', level: '1', path: 'Txu'},
  {id: '107', name: '男士大衣', money: '200/件', level: '2', path: 'manCoat'},
  {id: '108', name: '女士大衣', money: '300/件', level: '2', path: 'womenCoat'},
  {id: '109', name: '牛肉', money: '40/斤', level: '1', path: 'cow'},
  {id: '110', name: '鸡肉', money: '15/斤', level: '1', path: 'chicken'},
  {id: '111', name: '电视', money: '2000/台', level: '2', path: 'tv'},
  {id: '112', name: '冰箱', money: '3888/台', level: '2', path: 'refrigerator'},
  {id: '113', name: '运动鞋', money: '120/双', level: '2', path: 'shoes'},
  {id: '114', name: '电脑', money: '5000/台', level: '2', path: 'computer'},
  {id: '115', name: 'web前端', money: '20/本', level: '0', path: 'book'},
]
app.get("/goods", (req, res) => {
  res.send({
    data: goodList,
    meta: {
      msg: "获取权限列表成功",
      status: 200
    }
  })
})
/*添加商品表单*/
app.post("/addGoods", (req, res) => {
  let userMsg = req.body.userMsg;
  goodList.push(userMsg)
  res.send({
    data: {
      users: goodList
    },
    meta: {
      msg: "添加数据成功",
      status: 200
    }
  })
})
//商品柱状图
app.get("/coatGood",(req,res)=>{
 res.send(
   {
     options: {
       title: {
         text: '商品柱状图'
       },
       tooltip: {

       },
       legend: {
         data: ['销量']
       },
       toolbox: {
         feature: {
           saveAsImage: {}
         }
       },
       xAxis: {
         data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
       },
       yAxis: {},
       series: [
         {
           name: '销量',
           type: 'bar',
           data: [5, 20, 36, 10, 10, 20]
         }
       ]

     }
   }
 )
})
//水果折线图
app.get("/fruitGood",(req,res)=>{
 res.send({
   options: {
     title: {
       text: '水果销售趋向'
     },
     tooltip: {
       trigger: 'axis'
     },
     legend: {
       data: ['苹果', '芒果', '西瓜', '草莓', '柠檬']
     },
     grid: {
       left: '2%',
       right: '2%',
       bottom: '2%',
       containLabel: true
     },
     toolbox: {
       feature: {
         saveAsImage: {}
       }
     },
     xAxis: {
       type: 'category',
       boundaryGap: false,
       data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月']
     },
     yAxis: {
       type: 'value'
     },
     series:  [
       {
         name: '苹果',
         type: 'line',
         stack: 'Total1', //stack数据堆叠 如果值相等就会将前一个值与第二个值相加=第二个值
         data: [120, 132, 101, 134, 90, 230, 210]
       },
       {
         name: '芒果',
         type: 'line',
         stack: 'Total2',
         data: [220, 182, 191, 234, 290, 330, 310]
       },
       {
         name: '西瓜',
         type: 'line',
         stack: 'Total3',
         data: [150, 232, 201, 154, 190, 330, 410]
       },
       {
         name: '草莓',
         type: 'line',
         stack: 'Total3',
         data: [320, 332, 301, 334, 390, 330, 320]
       },
       {
         name: '柠檬',
         type: 'line',
         stack: 'Total4',
         data: [820, 932, 901, 934, 1290, 1330, 1320]
       }
     ]
   }
 })
})
//家居饼图
app.get("/homeGood",(req,res)=>{
  res.send({
    options:{
      title: {
        text: '家电本月销售占比'
      },
      legend: {
        top: 'bottom'
      },
      toolbox: {
        show: true,
        feature: {
          mark: { show: true },
          dataView: { show: true, readOnly: false },
          restore: { show: true },
          saveAsImage: { show: true }
        }
      },
      series: [
        {
          name: '家电数据展示',
          type: 'pie',
          radius: [40, 120],
          center: ['50%', '50%'],
          roseType: 'area',
          itemStyle: {
            borderRadius: 8
          },
          data: [
            { value: 40, name: '电脑' },
            { value: 38, name: '冰箱' },
            { value: 32, name: '空调' },
            { value: 30, name: '洗衣机' },
            { value: 28, name: '电视机' },

          ]
        }
      ]
    }
  })
})
//护肤品状图
app.get('/skinGood',(req,res)=>{
  res.send({
    options:{
      title: {
        text: '护肤品销量'
      },
      toolbox: {
        feature: {
          saveAsImage: {}
        }
      },
      legend: {},
      tooltip: {},
      dataset: {
        dimensions: ['product', '2018', '2019', '2020'],
        source: [
          { product: '香奈儿', 2018: 43.3, 2019: 85.8, 2020: 93.7 },
          { product: '薇薇儿', 2018: 83.1, 2019: 73.4, 2020: 55.1 },
          { product: '雅丹妮', 2018: 86.4, 2019: 65.2, 2020: 82.5 },
          { product: '爱慕尔', 2018: 72.4, 2019: 53.9, 2020: 39.1 }
        ]
      },
      xAxis: { type: 'category' },
      yAxis: {},
      series: [{ type: 'bar' }, { type: 'bar' }, { type: 'bar' }]
    }
  })
})
// 快递信息
const progressMessage=[
  {timestamp:'2022/4/11',goodName:'已提交订单,商家确认发货',userInfo:'王小虎 提交于 2018/4/11 20:46'},
  {timestamp:'2022/4/12',goodName:'正在通知韵达快递取件',userInfo:'王小虎 提交于 2018/4/12 8:46'},
  {timestamp:'2022/4/13',goodName:'广东广州番禺公司 刘喜庆',userInfo:'王小虎 提交于 2018/4/13 10:22'},
  {timestamp:'2022/4/13',goodName:'广东佛山交付中心',userInfo:'王小虎 提交于 2018/4/13 15:33'},
  {timestamp:'2022/4/14',goodName:'河南郑州分拨交付中心',userInfo:'王小虎 提交于 2018/4/14 16:20'},
  {timestamp:'2022/4/14',goodName:'河南漯河分拨交付中心',userInfo:'王小虎 提交于 2018/4/14 9:46'},
  {timestamp:'2022/4/15',goodName:'已离开河南漯河分拨交付中心',userInfo:'王小虎 提交于 2018/4/15 12:22'},
  {timestamp:'2022/4/15',goodName:'快递到达指定位置,请用户尽快取件',userInfo:'王小虎 提交于 2018/4/15 13:40'}
]
app.get('/kuaidi',(req,res)=>{
  res.send({
    data:progressMessage,
    meta: {
      msg: "添加数据成功",
      status: 200
    }
  })
})
//启动监听
app.listen(3000, () => {
  console.log("running...")
})
